package aliyun.cx.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import aliyun.cx.admin.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
    
    /**
     * 分页查询文章列表（包含作者和分类信息）
     */
    @Select("SELECT a.*, u.nickname as author_name, c.name as category_name " +
            "FROM article a " +
            "LEFT JOIN users u ON a.author_id = u.id " +
            "LEFT JOIN category c ON a.category_id = c.id " +
            "ORDER BY a.create_time DESC")
    Page<Article> selectArticlePage(Page<Article> page);
    
    /**
     * 根据ID查询文章（包含作者和分类信息）
     */
    @Select("SELECT a.*, u.nickname as author_name, c.name as category_name " +
            "FROM article a " +
            "LEFT JOIN users u ON a.author_id = u.id " +
            "LEFT JOIN category c ON a.category_id = c.id " +
            "WHERE a.id = #{id}")
    Article selectArticleById(Long id);
    
    /**
     * 根据分类ID查询文章数量
     */
    @Select("SELECT COUNT(*) FROM article WHERE category_id = #{categoryId}")
    Long countByCategoryId(Long categoryId);
}
